package com.sugon.modules.device.listener;

import cn.hutool.core.util.StrUtil;
import com.sugon.entity.DevMaintenRepairEntity;
import com.sugon.entity.DevMaintenWorkEntity;
import com.sugon.modules.base.consts.CacheKeyPrefix;
import com.sugon.service.DevMaintenRepairService;
import com.sugon.service.DevMaintenWorkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Slf4j
public class MyRedisKeyExpiredMessageDelegate extends KeyExpirationEventMessageListener {

    @Autowired
    private DevMaintenRepairService devMaintenRepairService;
    @Autowired
    private DevMaintenWorkService devMaintenWorkService;

    public MyRedisKeyExpiredMessageDelegate(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 针对 redis 数据失效事件，进行数据处理
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredMsg = new String(message.getBody());
        if (StrUtil.isEmpty(expiredMsg)) {
            return;
        }
        System.out.println(expiredMsg);
        if (StrUtil.startWith(expiredMsg, CacheKeyPrefix.DRT_WORKSHOP_TRIGGER_PREFIX)) {
            String repairTicketId = expiredMsg.replace(CacheKeyPrefix.DRT_WORKSHOP_TRIGGER_PREFIX, "");
            /*
                车间帮助触发
                校验工单是否已经上报到车间机电设备工程师，如果没有则修改维修工单超时状态
             */
            log.info("维修工单维修工处理时长超期，自动触发车间协助，工单ID: " + repairTicketId);
            DevMaintenRepairEntity devMaintenRepair = devMaintenRepairService.queryObject(repairTicketId);
            Assert.notNull(devMaintenRepair, "维修记录不存在！");
            // 校验当前维修记录协助等级或者查看是否已指定二级协助人，如果都没有，那么标记工单为超时状态
            DevMaintenWorkEntity work = new DevMaintenWorkEntity();
            work.setId(repairTicketId);
            if (devMaintenRepair.getOrderNo() == 1 || StrUtil.isEmpty(devMaintenRepair.getLevel2AssistUser())) {
                work.setIsOvertime(1);
            } else {
                work.setIsOvertime(0);
            }
            devMaintenWorkService.update(work);
        } else if (StrUtil.startWith(expiredMsg, CacheKeyPrefix.DRT_FACTORY_TRIGGER_PREFIX)) {
            /*
                工厂帮助触发
                校验工单是否已经上报到制造处，如果没有则由程序主动上报
             */
            String repairTicketId = expiredMsg.replace(CacheKeyPrefix.DRT_FACTORY_TRIGGER_PREFIX, "");
            log.info("维修工单维修工处理时长超期，自动触发工厂协助，工单ID: " + repairTicketId);
            DevMaintenRepairEntity devMaintenRepair = devMaintenRepairService.queryObject(repairTicketId);
            Assert.notNull(devMaintenRepair, "维修记录不存在！");
            // 校验当前维修记录协助等级或者查看是否已指定二级协助人，如果都没有，那么标记工单为超时状态
            DevMaintenWorkEntity work = new DevMaintenWorkEntity();
            work.setId(repairTicketId);
            if (devMaintenRepair.getOrderNo() == 2 || StrUtil.isEmpty(devMaintenRepair.getLevel3AssistUser())) {
                work.setIsOvertime(1);
            } else {
                work.setIsOvertime(0);
            }
            devMaintenWorkService.update(work);
        }

    }
}
